package com.yeswater.multithread.chapter1.test1_12.test4;

import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * author : yeswater
 * date : 2023/10/19
 */

public class RunTest {

    @Test
    public void test1() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("begin " + System.currentTimeMillis());
            LockSupport.park(); // 代替 suspend()
            System.out.println("  end " + System.currentTimeMillis());
        });

        t1.start();
        TimeUnit.SECONDS.sleep(4);
        LockSupport.unpark(t1); // 代替 resume()
    }

    @Test
    public void test2() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                System.out.println("begin " + System.currentTimeMillis());
                TimeUnit.SECONDS.sleep(5);
                System.out.println("=== park ===");
                LockSupport.park(); // 代替 suspend()
                System.out.println("  end " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();

        TimeUnit.SECONDS.sleep(2);
        LockSupport.unpark(t1);
    }

}
